package serv;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.annotation.WebFilter;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

/**
 * Servlet Filter implementation class UriFilter
 */
@WebFilter(description = "过滤器", urlPatterns = { "/UriFilter","*.html","*.jsp" })
public class UriFilter implements Filter {

	FilterConfig filterConfig = null;
    /**
     * Default constructor. 
     */
    public UriFilter() {
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see Filter#destroy()
	 */
	@Override
	public void destroy() {
		// TODO Auto-generated method stub
	}

	/**
	 * @see Filter#doFilter(ServletRequest, ServletResponse, FilterChain)
	 */
	@Override
	public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
		// TODO Auto-generated method stub
		
		String address = request.getRemoteAddr();
		//System.out.println("doFilter: ");
		filterConfig.getServletContext().log("User IP: " + address);
		String uri = ((HttpServletRequest) request).getRequestURI();
		filterConfig.getServletContext().log("URI: " + uri);
		uri=uri.substring(uri.lastIndexOf('/')+1);
		//忽略index logon login 开头的
		System.out.println("doFilter uri: " + uri);
		if(!uri.regionMatches(true, 0, "index", 0, 5)&&
				!uri.regionMatches(true, 0, "logon", 0, 5)&&
				uri.length()>0&&
				!uri.regionMatches(true, 0, "login", 0, 5))
		{
			//System.out.println("go here");
			
			HttpSession session = ((HttpServletRequest) request).getSession(false);
			((HttpServletResponse)response).setContentType("text/html");
			((HttpServletResponse)response).setCharacterEncoding("GBK");
			
			if (session == null){
				//用户无效重新登陆
				filterConfig.getServletContext().log("session invalid.relogin:");
				PrintWriter out = ((HttpServletResponse)response).getWriter();
				ReError(out);
				return;
			}else{
				/*判断是否有用户
				String UserID = (String) session.getAttribute("user_name");
				System.out.println("user_name:" + UserID);
				if(UserID==null){
					PrintWriter out = ((HttpServletResponse)response).getWriter();
					ReError(out);
					return;
				}*/
				//判断是否有机构信息
				GwkOrgan gwko =  (GwkOrgan) session.getAttribute("logonOrgan");
				//System.out.println("user_name:" + UserID);
				if(gwko==null){
					PrintWriter out = ((HttpServletResponse)response).getWriter();
					ReError(out);
					session.invalidate();//清除session
					return;
				}
			}
		}
		chain.doFilter(request, response);
	}

	/**
	 * @see Filter#init(FilterConfig)
	 */
	@Override
	public void init(FilterConfig fConfig) throws ServletException {
		// TODO Auto-generated method stub
		filterConfig = fConfig;
	}

	void ReError(PrintWriter out) throws IOException {
		GwkDB gd = new GwkDB();
		String webroot = gd.GetPrivateProfileString("WEB_ROOT","/gxgwk");
		out.print("<script language='javascript'>alert('请重新登录 ');window.location.href='");
		out.print(webroot);
		System.out.println("web root:"+webroot);
		out.print("/index.html';</script>");
		return;
	}
}
